298A - Snow Footprints - CodeForces Solution


greedy implementation *1300

Please click on ads to support us..

Python Code:

n = int(input())
seq = input()

rBegin = rEnd = lBegin = lEnd = 0
R = L = False

for i in range(n):
    if seq[i] == 'R':
        if R == False:
            R = True
            rBegin = i+1
        rEnd = i+1
    elif seq[i] == 'L':
        if L == False:
            L = True
            lBegin = i+1
        lEnd = i+1

if R==True and L==True:
    print(rBegin, lBegin-1)
elif R==True:
    print(rBegin, rEnd+1)
else:
    print(lEnd, lBegin-1)

C++ Code:

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>

#define test_case int t;cin>>t;while(t--)
#define ll long long
#define endl "\n"
#define all(v) v.begin(),v.end()

using namespace std;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

string base_converter(ll x,ll y)
{
    char buffer [33];
    itoa (x,buffer,y);
    string s=buffer;
    return s;
}
string findSum(string str1, string str2)
{
    // Before proceeding further, make sure length
    // of str2 is larger.
    if (str1.length() > str2.length())
        swap(str1, str2);

    // Take an empty string for storing result
    string str = "";

    // Calculate length of both string
    int n1 = str1.length(), n2 = str2.length();

    // Reverse both of strings
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());

    int carry = 0;
    for (int i=0; i<n1; i++)
    {
        // Do school mathematics, compute sum of
        // current digits and carry
        int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');

        // Calculate carry for next step
        carry = sum/10;
    }

    // Add remaining digits of larger number
    for (int i=n1; i<n2; i++)
    {
        int sum = ((str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');
        carry = sum/10;
    }

    // Add remaining carry
    if (carry)
        str.push_back(carry+'0');

    // reverse resultant string
    reverse(str.begin(), str.end());

    return str;
}

long long gcd(long long a, long long b) {
    return a ? gcd(b % a, a) : b;
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b;
}

long long fact(long long n) {
    long long f = 1;
    for (int i = 2; i <= n; i++)
    {
        f *= i;
    }
    return f;
}
long long ncr(long long n, long long r) {
    return fact(n) / (fact(r) * fact(n - r));
}
long long npr(long long n, long long r) {
    return fact(n) / fact(n - r);
}

bool prime (long long x)
{
    long long i=2;
    bool ch=true;
    if (x==1)
        ch=false;
    while (x>=i*i)
    {
        if (x%i==0)
        {
            ch=false;
            break;
        }
        i++;
    }
    if (ch==false)
    {
        return false;
    }
    else
        return true;
}


int main()
{
int f=0 , l = 0;
    int size ; cin >> size ;
    string str ; cin >> str ;
    for(int i=0 ; i<size ;i++) {
        if (str[i] != '.') {
            f = i;
            break;
        }
    }
    for(int i=size-1 ; i>=0 ;i--) {
        if (str[i] != '.') {
            l = i;
            break;
        }
    }

    if(str[f]==str[l] and str[l]=='R')
        cout << f+1 << " " << l+1+1 ;
    else if(str[f]==str[l] and str[l]=='L')
        cout << l+1 << " " << f ;
    else
    {
        for(int i=f ; i<size ; i++)
        {
            l = i ;
            if(str[i]=='L')
                break;
        }
        cout << ++f << " " << l ;
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST